package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.interf.PerfectClickListener;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.R;
import com.slwish.xty.entity.CommentLabelModel;
import com.slwish.xty.mvp.contract.CommentEditContract;
import com.slwish.xty.mvp.presenter.CommentEditPresenter;
import com.slwish.xty.widget.RatingBar;
import com.slwish.xty.widget.flowlayout.FlowLayout;
import com.slwish.xty.widget.flowlayout.TagAdapter;
import com.slwish.xty.widget.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

/**
 * 评价提交页
 */
public class CommentEditActivity extends PresenterToolbarActivity<CommentEditContract.Presenter>
        implements CommentEditContract.View {

    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.et_comment)
    EditText etComment;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ratingbar)
    RatingBar ratingbar;
    private String orderId;//订单号
    private String appraiser;//商家
    private Set<Integer> selectPosSet = new HashSet<>();//保存的选中的标签的ID
    private int ratingCounts;//评价星级1~5
    private long userId;
    private List<CommentLabelModel> commentLabelModels;

    @Override
    protected int getLayoutId() {
        return R.layout.act_comment_edit;
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.comment_title);
        setRightText(R.string.submit_text);
        Bundle bundle = getIntent().getExtras();
        userId = SpUtils.getLong(mContext, Contants.UserCode, 0);
        if (bundle != null) {
            orderId = bundle.getString("orderId");
            appraiser = bundle.getString("appraiser");
            tvName.setText(appraiser);
        }

        //查询评价标签
        mPresenter.queryCommentLabel();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ratingbar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                ratingCounts = (int) ratingCount;
            }
        });
        /**
         * 提交
         */
        setRightOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                String serviceItem = "";
                if (commentLabelModels != null && commentLabelModels.size() > 0 && selectPosSet != null && selectPosSet.size() > 0) {
                    Iterator it = selectPosSet.iterator(); // 获得一个迭代子
                    while (it.hasNext()) {
                        int obj = (int) it.next(); // 得到下一个元素
                        commentLabelModels.get(obj).getId();
                        serviceItem = serviceItem + commentLabelModels.get(obj).getId() + ",";
                    }
                    serviceItem = serviceItem.substring(0, serviceItem.length() - 1);
                }
                mPresenter.addComment(orderId, userId, etComment.getText().toString()
                        , etComment.getText().toString(), serviceItem, appraiser, "");
            }
        });
        flowlayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                CommentEditActivity.this.selectPosSet = selectPosSet;

            }
        });
    }

    @Override
    public void queryCommentLabelSuccess(List<CommentLabelModel> model) {
        try {
            commentLabelModels = model;
            List<String> mLists = new ArrayList<>();
            for (CommentLabelModel servicrItems : model) {
                mLists.add(servicrItems.getLabelName());
            }
            flowlayout.removeAllViews();
            flowlayout.setAdapter(new TagAdapter<String>(mLists) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.view_comment_label_items_text,
                            parent, false);
                    view.setText(s);
                    return view;
                }
            });
        } catch (Exception e) {
        }
    }

    /**
     * 新增评论成功
     *
     * @param model
     */
    @Override
    public void addCommentSuccess(String model) {

    }

    @Override
    protected CommentEditContract.Presenter initPresenter() {
        return new CommentEditPresenter(this);
    }
}
